package code._20stream._01introduction;

import code._20stream.bean.Student;
import code._20stream.bean.StudentList;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Demo {
    @Test
    public void test1() {
        List<Student> studentList = new StudentList().studentList;
        ArrayList<Student> students = new ArrayList<>();
        for (Student student : studentList) {
            if (student.getAddress() == Student.Address.BJ) {
                students.add(student);
            }
        }
        System.out.println("students = " + students);

        //排序
        students.sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getHeight() - o1.getHeight();
            }
        });

        Student student1 = students.get(0);
        Student student2 = students.get(1);
        Student student3 = students.get(2);
        System.out.println(student1);
        System.out.println(student2);
        System.out.println(student3);
    }

    @Test
    public void test2() {
        List<Student> studentList = new StudentList().studentList;
        //创建有序集合、定下比较规则
        TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getHeight() - o1.getHeight();
            }
        });
        //遍历
        for (Student student : studentList) {
            if (student.getAddress() == Student.Address.BJ) {
                set.add(student);
            }
        }

        //取前三
        Student student1 = set.pollFirst();
        Student student2 = set.pollFirst();
        Student student3 = set.pollFirst();
        System.out.println(student1);
        System.out.println(student2);
        System.out.println(student3);
    }

    @Test
    public void test3() {
        List<Student> studentList = new StudentList().studentList;
        List<Student> result = studentList.stream()
                .filter(new Predicate<Student>() {
                    @Override
                    public boolean test(Student student) {
                        return student.getAddress() == Student.Address.BJ;
                    }
                })
                .sorted(new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o2.getHeight() - o1.getHeight();
                    }
                })
                .limit(3)
                .toList();
//        System.out.println(result);
        result.forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println(student);
            }
        });
    }
}
